package gen

import (
	"bytes"
	"fmt"
	"io/ioutil"

	"gopkg.in/goyy/goyy.v0/util/strings"
)

var serviceTemplate = `/** 
* go to java mybatis gen 
* @author hongyanyang1 
*/
package {{.Package}}.{{.ServicePkg}}{{moduleCrePkg .}};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import {{.Package}}.{{.BeanPkg}}{{moduleCrePkg .}}.{{camel .Bean}};
import {{.Package}}.{{.DaoPkg}}{{moduleCrePkg .}}.{{camel .Bean}}Dao;
import {{.Package}}.{{.ServicePkg}}.helper.BaseService;

@Service
public class {{camel .Bean}}Service extends BaseService<{{camel .Bean}}Dao,{{camel .Bean}}> {
	@SuppressWarnings("unused")
	@Autowired
	private {{camel .Bean}}Dao {{aamel .Bean}}Dao;
	
}
`

func GenService(genModules []Module) {
	fmt.Println("开始生成 Service...")
	for _, genModule := range genModules {
		if genModule.IsGenService {
			var project = genModule.SProject
			var cloums = genModule.Cloums
			var dir = PackageDir(genModule, "service")
			MkdirAll(dir)
			for tableName, cloums := range cloums {
				buf := bytes.Buffer{}
				var tempData = NewTempData(tableName, cloums, project, genModule)
				templ := NewTmpl(serviceTemplate)
				templ.Execute(&buf, tempData)
				fmt.Println(buf.String())
				err := ioutil.WriteFile(dir+"\\"+strings.Camel(tempData.Bean)+"Service.java", buf.Bytes(), 0755)
				if err != nil {
					fmt.Println(err.Error())
				}
			}
		}
	}
}
